

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>开启头部工具栏 - 数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>组件</cite></a>
      <a><cite>数据表格</cite></a>
      <a><cite>开启头部工具栏</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">通用插件</div>
          <div class="layui-card-body">
              <blockquote class="layui-elem-quote">Tips：点击列表中排序值，可进行重新排序哟！</blockquote>
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
              <div class="layui-form-item">
                
                <div class="layui-inline">
                  <label class="layui-form-label">关键字</label>
                  <div class="layui-input-inline">
                    <input type="text" name="keywords" id="keywords" placeholder="请输入标题关键字" autocomplete="off" class="layui-input">
                  </div>
                </div>
                <div class="layui-inline">
                  <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search_done" id="search_done">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                  </button>
                </div>
              </div>
            </div>
            <table id="list-table" lay-filter="test-table-toolbar">
              <thead>
                <tr>
                  <th lay-data="{type:'checkbox',fixed: 'left', }"></th>
                  <th lay-data="{field:'id', width:80,fixed: 'left', unresize: true, sort: true}">ID</th>
                  <th lay-data="{field:'title'}">标题</th>
                  <th lay-data="{field:'content'}">内容</th>
                  <!-- <th lay-data="{field:'createtime',width:120, sort: true}">创建时间</th> -->
                  <th lay-data="{field:'sort',width:100, sort: true,edit: 'text'}">排序</th>
                  <th lay-data="{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:150}">操作</th>
                </tr> 
              </thead>
              <tbody>
                {volist name="mess" id="v"}
                <tr>
                  <td><input type="checkbox" name=""></td>
                  <td>{$v.id}</td>
                  <td>{$v.title}</td>
                  <td>{$v.content}</td>
                  <!-- <td>{$v.createtime}</td> -->
                  <td>{$v.sort}</td>
                  <td>
                <!--     <a class="layui-btn " lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger " lay-event="del">删除</a> -->
                  </td>
                </tr>
                {/volist}                
              </tbody>
            </table>
            <!-- <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table> -->
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
                <!-- <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button> -->
                <!-- <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button> -->
                <button class="layui-btn layui-btn-sm" lay-event="add">添加内容</button>
              </div>
            </script>
             
            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="/static/layadmin/layui/layui.js"></script>  
  <script>
  layui.config({
    base: '/static/layadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table','form','laypage'], function(){
    var admin = layui.admin
    ,table = layui.table
    ,laypage  = layui.laypage 
    ,$ = layui.$
    ,form = layui.form;
    
    table.init('test-table-toolbar', {
      // height: 315 //设置高度
      // ,limit: 10 //注意：请务必确保 limit 参数（默认：10）是与你服务端限定的数据条数一致
      //支持所有基础参数
      toolbar: '#test-table-toolbar-toolbarDemo'
      ,page: true //开启分页
      // ,limit: 3
    }); 

    
    //头工具栏事件
    table.on('toolbar(test-table-toolbar)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
          var data = checkStatus.data;
          layer.alert(JSON.stringify(data));
        break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
          layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
        case 'add':
        layer.open({
          type: 2, 
          title: '添加', 
          content: '/addons/general/addArticle', //这里content是一个普通的String
          area: ['100%', '100%']
        });
      };
    });
    
    //监听行工具事件
    table.on('tool(test-table-toolbar)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
          $.get('{:url("addons/general/index_del")}',{id:data.id},function(ret){
              ret = $.parseJSON(ret);
              if (ret.code == 0) { //添加失败
                  layer.msg(ret.msg, { icon: 5 })
              } else { //添加成功
                  layer.msg(ret.msg, { icon: 6 }, function() {
                      // $(obj).parents("tr").remove();
                      obj.del();
                  })
              }
          });          
          layer.close(index);
        });
      } else if(obj.event === 'edit'){   
        layer.open({
          type: 2, 
          title: '修改', 
          content: '/addons/general/addArticle/id/'+data.id, //这里content是一个普通的String
          area: ['100%', '100%']
        });        
      }
    });
    //监听单元格编辑
    table.on('edit(test-table-toolbar)', function(obj){
        var value = obj.value //得到修改后的值
        ,data = obj.data //得到所在行所有键值
        ,field = obj.field; //得到字段
        // console.log(data.id);
        $.get('{:url("addons/general/cateSort")}',{id:data.id,sort:value},function(ret){
              ret = $.parseJSON(ret);
              // console.log(ret);
              if (ret.code == 0) { //添加失败
                  layer.msg(ret.msg, { icon: 5 },function(){
                      window.location.reload();
                  });
              } else { //添加成功
                  layer.msg(ret.msg, { icon: 6 }, function() {
                      window.location.reload();
                  })
              }
          });  
    });
    $('.showimg').on('click', function () {
              // 搜索条件
          var thumb = $(this).attr("src");
          window.open(thumb);
          // var turl;
          // if(thumb){
          //   // turl="/uploads/"+thumb;
          //   layer.open({
          //     type: 1, 
          //     title: '图片查看', 
          //     skin:'text-align',
          //     content: '<img src="'+thumb+'" style="height:100%;width:auto;"/>', //这里content是一个普通的String
          //     area: ['100%', '100%']
          //   });
          // }
        });
    // 执行搜索，表格重载
    $('#search_done').on('click', function () {
      // 搜索条件
      var keywords = $('#keywords').val();
      window.location.href="/addons/general/index?keywords="+keywords;
    });

  });
  </script>
</body>
</html>